import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:xmshop/app/services/httpsClient.dart';
import '../../../../models/message_model.dart';
import '../../../../services/storage.dart';
import '../../../../models/user_model.dart';
import '../../../../modules/user/controllers/user_controller.dart';

class PassLoginController extends GetxController {
  UserController userController = Get.find<UserController>();
  TextEditingController telController = TextEditingController();
  TextEditingController passController = TextEditingController();
  HttpsClient httpsClient = HttpsClient();

  @override
  void onClose() {
    // 更新用户状态
    userController.getUserInfo();
    super.onClose();
  }

  Future<MessageModel> doLogin() async {
    var response = await httpsClient.post("api/doLogin", data: {
      "username": telController.text,
      "password": passController.text,
    });
    if (response != null) {
      if (response.data["success"]) {
        // 保存用户信息到本地
        Storage.setData(
            "userInfo", UserModel.fromJson(response.data["userinfo"][0]));

        return MessageModel(message: "登录成功", success: true);
      }
      return MessageModel(message: response.data["message"], success: false);
    } else {
      return MessageModel(message: "网络异常", success: false);
    }
  }
}
